package gui;

import communications.Command;
import communications.GlobalObjectId;
import communications.ObjectDB;
import communications.PeerCommunicator;
import java.io.ObjectStreamException;
import java.io.Serializable;

public class NetworkPrinterProxy extends NetworkPrinter implements Serializable {

    /**
	 * 
	 */
	private static final long serialVersionUID = -4594894739460186042L;
	private GlobalObjectId globalObjectId;

    public NetworkPrinterProxy(GlobalObjectId globalObjectId){
    	super();
    	
        this.globalObjectId = globalObjectId;
    }

	@Override
	public void print(Object message)
	{
        String[] parameterTypeNames = new String[1];
        parameterTypeNames[0] = "java.lang.Object";
        Object[] actualParameters = new Object[1];
        actualParameters[0] = message;
        Command command = new Command(globalObjectId.getLocalObjectId(), "gui.NetworkPrinter", "print", parameterTypeNames, actualParameters, false);
        PeerCommunicator.getSingleton().sendASynchronous(globalObjectId, command);
	}

	@Override
	public Object writeReplace() throws ObjectStreamException
	{
		return new NetworkPrinterProxy(globalObjectId);
	}
	
	public Object readResolve() throws ObjectStreamException
	{
		if (globalObjectId.onCurrentMachine())
		{
			return ObjectDB.getSingleton().getValue(globalObjectId.getLocalObjectId());	
		}
		else
		{
			return this;
		}
	}
}
